﻿using GroupThreeMESProject.DTO.ProcedureDtos;
using GroupThreeMESProject.Entity;
using GroupThreeMESProject.Result;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using static GroupThreeMESProject.Service.ProductionPlan.ProductionPlanList;

namespace GroupThreeMESProject.WorkingProcedureService
{
    [ApiExplorerSettings(GroupName = "v2")]
    public class WorkingProcedureDataService : ApplicationService
    {
        private readonly
            ISqlSugarClient _db;

        public WorkingProcedureDataService(ISqlSugarClient db)
        {
            _db = db;
        }
        [HttpGet("GetWorkingProcedureList")]
        public async Task<ApiResult<List<Mes_WorkingProcedure>>> GetWorkingProcedureList()
        {
            var list = await _db.Queryable<Mes_WorkingProcedure>().ToListAsync();
            return ApiResult<List<Mes_WorkingProcedure>>.Success(list);
        }
        [HttpPost("GetProcedureBindList")]
        public async Task<ApiResult<List<ProcedureBindEquipmentDto>>> GetProcedureBindList(ProcedureBindQueryId info)
        {
            var list = await _db.Queryable<Mes_WorkingProcedure>().Where(p => p.Id == info.ProcedureId)
                .LeftJoin<Mes_ProcedureEquipment>((a, b) => a.Id == b.procedure_id)
                .LeftJoin<MesEquipment>((a, b, c) => b.EquipmentId == c.EquipmentId)
                .Select((a, b, c) => new ProcedureBindEquipmentDto
                {
                    EquipmentId = c.EquipmentId,
                    EquipmentTypeName = c.EquipmentTypeName.ToString(),
                    EquipmentUseName = c.EquipmentUseName.ToString(),
                    EquipmentName = c.EquipmentName,
                    EquipmentState = c.EquipmentState == true ? "运行" : "空闲",
                    State = c.State == true ? "有效" : "失效",
                    EquipmentInfo = c.EquipmentInfo
                }).ToListAsync();
            return ApiResult<List<ProcedureBindEquipmentDto>>.Success(list);
        }
        [HttpGet("GetEquipmentList")]
        public async Task<ApiResult<List<ProcedureBindEquipmentDto>>> GetEquipmentList()
        {
            var list = await _db.Queryable<MesEquipment>()
                .Select(a => new ProcedureBindEquipmentDto
                {
                    EquipmentId = a.EquipmentId,
                    EquipmentTypeName = a.EquipmentTypeName.ToString(),
                    EquipmentUseName =  a.EquipmentUseName.ToString(),
                    EquipmentName = a.EquipmentName,
                    EquipmentState = a.EquipmentState == true ? "运行" : "空闲",
                    State = a.State == true ? "有效" : "失效",
                    EquipmentInfo = a.EquipmentInfo
                }).ToListAsync();
            return ApiResult<List<ProcedureBindEquipmentDto>>.Success(list);
        }

        [HttpPost("InsertProcedureBindEquipment")]
        public async Task<ApiResult<int>> InsertProcedureBindEquipment(InsertProcedureBindEquimentDtos info)
        {
            Mes_ProcedureEquipment pequipment = new Mes_ProcedureEquipment();
            pequipment.procedure_id=info.Procedure_Id;
            pequipment.EquipmentId=info.EquipmentId;
            var res = await _db.Insertable(pequipment).ExecuteCommandAsync();
            return ApiResult<int>.Success(res);
        }
    }
}
